package com.demo.config;

import com.demo.entity.SpotOrder;
import com.demo.service.SpotOrderService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class SpotOrderTools {
    //景点预约
    @Autowired
    SpotOrderService spotOrderService;

    @Tool(name = "景点预约，进行景点预约功能",value = "景点预约id（自动递增，无需填写）用户id（自动根据{{userId}}填写" +
            "用户id（自动根据{{userId}}填写）" +
            "景点名称" +
            "门票价格（可选）" +
            "预定时间（自动生成，无需修改）")
    public String addSpot(SpotOrder spotOrder) {
        log.info("进行景点预约功能");
        SpotOrder spotOrderExist = spotOrderService.getBySpotOrderId(spotOrder.getUserId());
        if(spotOrderExist != null) {
            return "您已经有景点预约记录了,记录如下："+spotOrderExist.toString();
        }
        spotOrder.setReserveTime(LocalDateTime.now());
        if(spotOrderService.save(spotOrder)){
            log.info("景点预约成功，预约信息为{}",spotOrder);
            return "预约成功,您的预约记录为"+spotOrder.toString();
        }
        return "预约失败";
    }

    @Tool(name = "查询景点预约",value = "根据用户的账户id{{userId}}")
    public String querySpot(@P(value = "用户的账户id{{userId}}",required = true) Long userId) {
        SpotOrder spotOrder = spotOrderService.getBySpotOrderId(userId);
        log.info("查询到预约，{}",spotOrder);
        if(spotOrder == null){
            return "没有预约，您可以先进行景点出行预约";
        }

        return "景点预约查询成功，预约记录如下："+spotOrder;
    }

    @Tool(name = "删除景点预约记录",value = "根据用户的账户id进行查询，账户id就是用户登录时的id，也就是{{userId}}，ai已经知道")
    public String deleteSpot(@P(value = "用户的账户id{{userId}}",required = true) Long userId) {
        SpotOrder spotOrder = spotOrderService.getBySpotOrderId(userId);
        if(spotOrder == null){
            return "没有预约记录";
        }
        if(spotOrderService.removeById(spotOrder.getId())){
            return "删除成功";
        }

        return "删除失败"+spotOrder;
    }

    @Tool(name = "修改景点预约记录",value = "根据预约记录的id进行修改，id是预约记录的唯一标识符\n" +
            "用户id（自动根据{{userId}}填写）\n" +
            "景点id（可选）\n" +
            "门票id（可选）\n" +
            "门票价格（可选）\n" +
            "预定时间（自动生成，无需修改）")
    public String updateSpot(
                             @P(value = "用户的账户id{{userId}}", required = true) Long userId,
                             @P(value = "景点名称（可选）", required = false) String spotName,
                             @P(value = "门票价格（可选）", required = false) Double ticketPrice) {
        SpotOrder spotOrder = spotOrderService.getBySpotOrderId(userId);
        if(spotOrder == null) {
            return "没有找到对应的预约记录";
        }

        // 更新预约信息
        if (spotName != null) spotOrder.setSpotName(spotName);
        if (ticketPrice != null) spotOrder.setTicketPrice(ticketPrice);
        spotOrder.setReserveTime(LocalDateTime.now());

        if(spotOrderService.updateById(spotOrder)){
            log.info("景点预约记录修改成功，新的预约信息为{}", spotOrder);
            return "修改成功，您的新的预约记录为"+spotOrder;
        }
        return "修改失败";
    }

}
